#include<iostream>

using namespace std;

long long sum;

int main()
{
	long long x,y;
	cin>>x>>y;
	while(x && y)
	{
		swap(x,y);
		sum+=4*y*(x/y);
		x%=y;
	}
	cout<<sum<<endl;
	return 0;
}
